package org.jeecgframework.web.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.web.service.UploadChangeRecordService;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.FileBean;
import org.jeecgframework.web.util.StaticValue;
import org.jeecgframework.web.util.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**   
 * @Title: UploadChangeRecordController
 * @Description: 上传备件更换记录
 * @author ZhangYunlong
 * @date 2016-04-01
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/uploadChangeRecordController")
public class UploadChangeRecordController extends BaseController {

	@Autowired
	private UploadChangeRecordService uploadChangeRecordService;
	
	@Autowired
	private SystemService systemService;
	/**
	 * 设备以及备件信息列表 页面跳转
	 * @return
	 */
	@RequestMapping(params = "uploadFile")
	public ModelAndView uploadFile(HttpServletRequest request) {
		return new ModelAndView("com/zybk/file_upload/uploadChangeRecord");
	}
	
	@ResponseBody
	@RequestMapping(params = "importExcel")
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		AjaxJson j = new AjaxJson();
		FileBean fileBean = UploadFileUtil.UploadFile(request);
		ZyFileLogEntity fileLog  = new ZyFileLogEntity();
		fileLog.setBusinessType(StaticValue.IMPORT_FILE_SPARE_RECORDER_BASE_DATA); //文件业务
		fileLog.setOldFileName(fileBean.getFileRealName());//文件原名
		fileLog.setNewFileName(fileBean.getFileName());//文件新名
		fileLog.setFilePath(fileBean.getSavePath());
		fileLog.setFileType("xlsx");
		fileLog.setStatus("0");
		systemService.save(fileLog);
		j.setObj(fileBean.getSavePath());
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-6-24 下午6:22:47
	 * Description 读取上传的excel 保存数据
	 */
	@RequestMapping(params = "readExcel", method = RequestMethod.POST)
	@ResponseBody
	public void readExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println("进入 保存 更换记录action>>>>>>>>>>>>>>>");
		uploadChangeRecordService.parseExcel(request);
	}
}
